<?php

class myDb
{
  private $db;
  public function __construct()
  {
	
    $params = array('host'     => 'localhost',//$dbconfig['hostname'],
		    'username' => 'xiaopei',//$dbconfig['username'],
		    'password' => '123456',//$dbconfig['password'],
		    'dbname'   => 'studydb1');//$dbconfig['database']);
    try 
      {
	$this->db = Zend_Db::factory('pdo_mysql', $params);
	$this->db->getConnection();
	$this->db->exec('set names utf8;');
	//$this->db->exec('set names latin1;');
      }
    catch (Zend_Db_Adapter_Exception $e)
      {
	print $e->getMessage();
      }
  }	
  public function getDb()
  {
    return $this->db;
  }
};

function get_real_price($ori_price, $dis_price)
{
  if ($ori_price == '0.00' || $dis_price == '0.00')
    return '免费';

  return '￥'.sprintf("%.2f", ($dis_price ? $dis_price : $ori_price ));
}


/* str like:
   desc: update page num to $num
   parameters: $str: cat_id=9&tag=C&page=8
  */
function update_page_num($str, $num)
{
  if (strpos($str, 'page=') === false)
    return $str."&page={$num}";
  
  return preg_replace("/page=\d+/", "page={$num}", $str);
}

/* return the value not null */
function get_true_value($a, $b)
{
  return $a ? $a : $b;
}

// param:
// array $opts, each item as key=>val,
function make_options(array $opts=null, $select=null)
{
  if (!isset($opts) || empty($opts))
    return "";
  $str = "<option value='0'>请选择</option>";
  foreach($opts as $key=>$val)
    {
      $sel = $key == $select ? 'selected' : '';
      $str .= "<option value='$key' $sel>$val</option>";
    }
  return $str;
}
